import type { InputInstance, SelectInstance } from '@arco-design/web-vue'
import type { Component, ComputedRef, Ref } from 'vue'

import type { PromiseFn } from '@/apis/schema/all'
import type { FilterItem } from '../strong-search'

/** 支持类型 */
export type CloudFilterWidget =
  | 'input'
  | 'select'
  | 'strong-search'
  | 'strong-date'
  | 'date'
  | 'date-range'

/** 支持类型 => 组件Map */
export type CloudFilterWidgetsMap = Record<CloudFilterWidget, Component>

type CloudFilterBarCommonProps = {
  class?: string
  defaultValue?: any
  span?: number
  field: string
  onChange?: (state?: CloudFilterBarState) => void
  disabled?: (state?: Ref<CloudFilterBarState>) => boolean
}

type InputWidgetProps = {
  widget: 'input'
  slots?: Partial<InputInstance['$slots']>
  showIcon?: boolean
} & Partial<Omit<InputInstance['$props'], 'modelValue' | 'onChange'>> &
  CloudFilterBarCommonProps

type SelectWidgetProps = {
  widget: 'select'
  fn?: PromiseFn
  allText?: string
  options?:
    | SelectInstance['$props']['options']
    | ComputedRef<SelectInstance['$props']['options']>
  slots?: Partial<SelectInstance['$slots']>
  relatedField?: string
} & Partial<
  Omit<
    SelectInstance['$props'],
    'modelValue' | 'disabled' | 'options' | 'onChange'
  >
> &
  CloudFilterBarCommonProps

export type StrongSearchWidgetProps = {
  widget: 'strong-search'
  placeholder?: string
  isFilter?: boolean
  options: FilterItem[]
} & Partial<CloudFilterBarCommonProps>

export type CloudFilterBarWidget = (
  | InputWidgetProps
  | SelectWidgetProps
  | StrongSearchWidgetProps
) &
  Pick<Partial<CloudFilterBarCommonProps>, 'defaultValue'>

export interface CloudFilterBarScheme {
  widgets?: CloudFilterBarWidget[]
}

export type CloudFilterBarState = Record<string, any>
